package dd.control;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

import dd.model.Event;
import dd.model.Model;
import dd.view.addEdgeFrame;

import xml.Message;

public class AddChoiceResponseController {
	/**
	 * this controller called when a choice response come, add an edge to the event. 
	 * repaint the screen
	 */
	HashSet<String> s;
	public void process(Message response){
		Model model = Model.getInstance();
		Event e = model.getEvent();
		s = e.getset();
		Node update = response.contents.getFirstChild();
		NamedNodeMap map = update.getAttributes();
		String responseid = map.getNamedItem("id").getNodeValue();
		String choice = map.getNamedItem("choice").getNodeValue();
		String numberr = map.getNamedItem("number").getNodeValue();
		System.out.println(numberr+choice);
		int number = Integer.parseInt(numberr);
		//if this choice is to my event
		if(responseid.equals(e.getEventID())){
			//if this number of choice is not added
			if(true){
				s.add(numberr);
				e.setChoice(number, choice);
				System.out.println(numberr+choice+e.getPostion());
				model.getaddAddChoiceScreen().setallchoice();
			}
			// if all the choices have been added
			if(s.size() ==e.getNumberLines()){
				addEdgeFrame aef = new addEdgeFrame();
				aef.setVisible(true);
				model.getaddAddChoiceScreen().dispose();
			}
		}
	}
}
